#!/usr/bin/perl

package IkiWiki::Plugin::opengraph;

use warnings;
use strict;
use IkiWiki 3.00;

our $VERSION = '0.1.4';

sub import {
	hook(type => "pagetemplate", id => "opengraph", call => \&opengraph_tags);
}

sub opengraph_tags {
	my %args = @_;
	${args}{template}->param('OPENGRAPH' => 1);
	my ${title} = pagetitle(${args}{destpage});
	my ${url} = urlto(${args}{destpage}, 'index', '1');
	my ${image} = urlto('logo.png', 'index', '1');
	my ${type} = pagetype(${args}{destpage});
	my ${opengraph_title} = ${title} || ${config}{'opengraph_title'} || "ikiwiki";
	my ${opengraph_description} = ${config}{'opengraph_description'} || "ikiwiki";
	my ${opengraph_type} = ${type} || ${config}{'opengraph_type'} || "website";
	my ${opengraph_image} = ${image} || ${config}{'opengraph_image'} || "http://ikiwiki.info/logo/ikiwiki.png";
	my ${opengraph_url} = ${url} || ${config}{'opengraph_url'} || "http://ikiwiki.info/";
	my ${opengraph_tags} =<<EOF;
<meta property="og:title" content="${opengraph_title}">
\t<meta property="og:description" content="${opengraph_description}"/>
\t<meta property="og:type" content="${opengraph_type}">
\t<meta property="og:image" content="${opengraph_image}">
\t<meta property="og:url" content="${opengraph_url}">
EOF
	${args}{template}->param('OPENGRAPH_TAGS' => ${opengraph_tags})
}

1;

__END__

=head1 NAME

IkiWiki::Plugin::opengraph - Adds Open Graph tags on the html head

=head1 DESCRIPTION

This plugin implements the Open Graph tags in the head of the hmtl for all
pages, provided you configure it properly and add it to the current
template. For more information on what is Open Graph, visit
<https://en.wikipedia.org/wiki/Open_Graph>. To test your site against the
Open Graph rules, use the tool available on
<https://developers.facebook.com/tools/debug/og/object/>.

WARNING: Open Graph is modern spyware. You should use this if and only if
you don't mind making the readers of your wiki/blog being tracked by evil
corporations without their consent. By using this plugin you are being mean
to the people who are reading your content. You have been warned.

=head1 INSTALLATION

Put F<opengraph.pm> in F<$HOME/.ikiwiki/IkiWiki/Plugin/> or elsewhere in
your C<@INC> path. Or read <http://ikiwiki.info/plugins/install/>.

=head1 CONFIGURATION

Add to the configuration in your F<blog.setup> file.

	## Open Graph plugin
	# For more information, see
	# <https://en.wikipedia.org/wiki/Open_Graph#Open_Graph_protocol>.
	# Default values for <http://ikiwiki.info>
	# obtained from <https://developers.facebook.com/tools/debug/og/object/>
	# meta property="og:title"
	opengraph_title: "ikiwiki"
	# meta property="og:type"
	opengraph_type: "website"
	# meta property="og:url"
	opengraph_url: "http://ikiwiki.info/"
	# meta property="og:image"
	opengraph_image: "http://ikiwiki.info/logo/ikiwiki.png"
	# meta property="og:description"
	opengraph_description: "Ikiwiki is a wiki compiler."

Add C<opengraph> to the list of plugins:

	add_plugins => [qw{goodstuff opengraph}],

=head1 TEMPLATES

You will need to add the following code to F<page.tmpl> on the current 
template. It **must** be in the <head> section of the <html>. I recommend 
puting it after the <title> tag.

 <TMPL_IF OPENGRAPH>
 <TMPL_VAR OPENGRAPH_TAGS>
 </TMPL_IF>

=head1 BUGS AND LIMITATIONS

...that's not a bug. It's an issue. Issues shall be reported at 
https://notabug.org/hiatobr/ikiwiki-plugin-opengraph/issues

Seriously, I don't know how to fetch the current page's description. Help on
 that is appreciated.

=head1 LICENSE AND COPYRIGHT

Copyleft (.) 2015 Hacklab Independência
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.

=head1 SEE ALSO

=over 4

=item https://ikiwiki.info/plugins/contrib/opengraph/

=item https://en.wikipedia.org/wiki/Open_Graph

=back
